﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QLKCB.Model;
using QLKCB.Common;
using System.Collections;

namespace QLKCB.Controller
{
    class cBenhNhan
    {

        ConnectDB connectDB = new ConnectDB();
        Utils utils = new Utils();

        public DataTable getDanhSach()
        {
            String sql = "select * from BenhNhan";
            object[] param = { };
            DataTable dtDanhSach = connectDB.ExeQuery(sql, param);
            return dtDanhSach;
        }

        public DataTable getMotBenhNhan(string maBenhNhan) 
        {
            String sql = "select * from BenhNhan where mabn=@mabn";
            object[] param = { "@mabn", maBenhNhan };
            DataTable dtDanhSach = connectDB.ExeQuery(sql, param);
            return dtDanhSach;
        }

        public DataTable timKiemBenhNhan(string strTimKiem)
        {
            String sql = "select * from BenhNhan where MaBN like '%' + @mabn + '%' or TenBN like '%' + @tenbn + '%'";
            object[] param = { "@mabn", strTimKiem, "@tenbn", strTimKiem };
            DataTable dtDanhSach = connectDB.ExeQuery(sql, param);
            return dtDanhSach;
        }

        public int themBenhNhan(string maBN, string tenBN, string doiTuong, string diaChi, string soDienThoai, DateTime namSinh, string gioiTinh, string danToc, string ngheNghiep, string noiLamViec)
        {

            string sql = "insert into BenhNhan values(@mabn, @tenbn, @doituong, @diachi, @sodt, @namsinh, @gioitinh, @dantoc, @nghenghiep, @noilamviec)";
            object[] param = { "@mabn", maBN, "@tenbn", tenBN, "@doituong", doiTuong, "@diachi", diaChi, "@sodt", soDienThoai, "@namsinh", namSinh, "@gioitinh", gioiTinh, "@dantoc", danToc, "@nghenghiep", ngheNghiep, "@noilamviec", noiLamViec };
            int result = connectDB.ExeNonQuery(sql, param);
            return result;

        }

        public int capNhatBenhNhan(string maBN, string tenBN, string doiTuong, string diaChi, string soDienThoai, DateTime namSinh, string gioiTinh, string danToc, string ngheNghiep, string noiLamViec)
        {
            string sql = "update BenhNhan set tenbn=@tenbn, doituong=@doituong, diachi=@diachi, sodienthoai=@sodt, namsinh=@namsinh, gioitinh=@gioitinh, dantoc=@dantoc, nghenghiep=@nghenghiep, noilamviec=@noilamviec where mabn=@mabn";
            object[] param = { "@mabn", maBN, "@tenbn", tenBN, "@doituong", doiTuong, "@diachi", diaChi, "@sodt", soDienThoai, "@namsinh", namSinh, "@gioitinh", gioiTinh, "@dantoc", danToc, "@nghenghiep", ngheNghiep, "@noilamviec", noiLamViec };
            int result = connectDB.ExeNonQuery(sql, param);
            return result;
        }

        public string getMaBenhNhan()
        {
            string prefixKey = "BN";
            DateTime today = DateTime.Today;
            String nowDate = today.ToString("yyyyMMdd");
            string sql = "select top 1 MaBN from BenhNhan where MaBN like @mabn + '%' order by mabn desc ";
            object[] param = { "@mabn", prefixKey + nowDate };

            string key = utils.genKey(sql, param, 2, prefixKey);
            return key;
        }

    }
}
